<?php

namespace app\common\model;

use think\Model;

class Sellers extends Model
{
    /**
     * 新增商家
     * @param $param
     * @return Admin
     */
    static function add_seller($param)
    {
        $seller = new self();
        $seller->user_id = $param['user_id'];
        $seller->head_img = isset($param['head_img']) ? $param['head_img'] : '';
        $seller->seller_name = isset($param['seller_name']) ? $param['seller_name'] : '';
        $seller->seller_address = isset($param['seller_address']) ? $param['seller_address'] : '';
        $seller->true_name = isset($param['true_name']) ? $param['true_name'] : '';
        $seller->telephone = isset($param['telephone']) ? $param['telephone'] : '';
        $seller->created_time = date("Y-m-d H:i", time());
        $seller->save();
        return $seller;
    }

    /**
     *编辑商家
     * @param $param
     * @return Admin
     */
    static function edit_seller($user_id, $param)
    {
        $s = new self();
        $seller = $s->where('user_id', $user_id)->find();
        $seller->user_id = $param['user_id'];
        isset($param['head_img']) ? $seller->head_img = $param['head_img'] : '';
        $seller->seller_name = isset($param['seller_name']) ? $param['seller_name'] : '';
        $seller->seller_address = isset($param['seller_address']) ? $param['seller_address'] : '';
        $seller->true_name = isset($param['true_name']) ? $param['true_name'] : '';
        $seller->telephone = isset($param['telephone']) ? $param['telephone'] : '';
        $seller->status = isset($param['status']) ? $param['status'] : 0;
        $seller->save();
        return $seller;
    }

    /**
     * 取消设置商家
     * @param $user_id
     */
    static function del_seller($user_id)
    {
        $s = new self();
        $seller = $s->where('user_id', $user_id)->find();
        $seller->delete();
    }

    /**
     * 根据id获取商家信息
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    static function get_seller_by_id($id)
    {
        $s = new self();
        $data = $s->find($id);
        return $data;
    }

    /**
     * 根据user_id获取商家信息
     * @param $user_id
     * @return array|false|\PDOStatement|string|Model
     */
    static function get_seller_by_user_id($user_id)
    {
        $data = Sellers::get(["user_id" => $user_id]);
        return $data;
    }

    /**
     * 当前商家的用户
     * @return bool|null|static
     */
    function get_user()
    {
        if (isset($this->user_id)) {
            return Users::get(["id" => $this->user_id]);
        }
        return false;
    }
}
